3A - Shortest path of the king - CodeForces Solution


greedy shortest paths *1000

Please click on ads to support us..

Python Code:

start = input()
end = input()
start = (ord(start[0]) - ord('a'), int(start[1]) - 1)
end = (ord(end[0]) - ord('a'), int(end[1]) - 1)
dx = end[0] - start[0]
dy = end[1] - start[1]
print(max(abs(dx), abs(dy)))
while dx or dy:
    move = ""
    if dx > 0:
        dx -= 1
        move += "R"
    if dx < 0:
        dx += 1
        move += "L"
    if dy > 0:
        dy -= 1
        move += "U"
    if dy < 0:
        dy += 1
        move += "D"
    print(move)

C++ Code:

#include <bits/stdc++.h>
using namespace std;
int main(){
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    string s,t;
    cin >> s >> t;
    cout << max(abs(s[0]-t[0]),abs(s[1]-t[1])) << "\n";
    while(s!=t){
        if(s[0]<t[0]){
            cout << 'R';
            s[0]++;
        }
        else if(s[0]>t[0]){
            cout << 'L';
            s[0]--;
        }
        if(s[1]<t[1]){
            cout << 'U';
            s[1]++;
        }
        else if(s[1]>t[1]){
            cout << 'D';
            s[1]--;
        }
        cout << "\n";
    }
    return 0;
}


Comments

Submit
0 Comments
More Questions

1635B - Avoid Local Maximums
20A - BerOS file system
1637A - Sorting Parts
509A - Maximum in Table
1647C - Madoka and Childish Pranks
689B - Mike and Shortcuts
379B - New Year Present
1498A - GCD Sum
1277C - As Simple as One and Two
1301A - Three Strings
460A - Vasya and Socks
1624C - Division by Two and Permutation
1288A - Deadline
1617A - Forbidden Subsequence
914A - Perfect Squares
873D - Merge Sort
1251A - Broken Keyboard
463B - Caisa and Pylons
584A - Olesya and Rodion
799A - Carrot Cakes
1569B - Chess Tournament
1047B - Cover Points
1381B - Unmerge
1256A - Payment Without Change
908B - New Year and Buggy Bot
979A - Pizza Pizza Pizza
731A - Night at the Museum
742A - Arpa’s hard exam and Mehrdad’s naive cheat
1492A - Three swimmers
1360E - Polygon